<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */

    /**
     * Defines the Role model of the application.
     *
     * @package     application
     * @subpackage  models
     *
     * @author NairuS
     * @version 1.2 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     */
    class RoleModel extends Model
    {
        /**
         * Defines the name of label property.
         *
         * @var string
         */
        const LABEL_FIELD = 'label';

        /**
         * Defines the name of rank property.
         *
         * @var string
         */
        const RANK_FIELD  = 'rank';

        /**
         * Defines the name of slug property.
         *
         * @var string
         */
        const SLUG_FIELD  = 'slug';

        /**
         * Invoked before the validate method.
         *
         * @return void
         */
        public function beforeValidate()
        {
            if( !$this->isNew() )
            {
                $rankValidator = $this->getPostValidator( self::RANK_FIELD );
                $slugValidator = $this->getPostValidator( self::SLUG_FIELD );
                $this->setOption( UniqueValidator::$exceptKey, array( self::ID_FIELD => $this->vo->id ), $rankValidator );
                $this->setOption( UniqueValidator::$exceptKey, array( self::ID_FIELD => $this->vo->id ), $slugValidator );
            }
            else
            {
                $this->setExcludeFields( array( self::ID_FIELD ) );
            }
        }

        /**
         * Configure the validation of this model.
         *
         * @param array $exludeFields
         * @return void
         */
        public function configure( $exludeFields = array() )
        {
            // defines the extra fields to not validate.
            parent::configure( $exludeFields ) ;

            // sets the validators.
            $stringOptions =  array( StringValidator::$maxLengthKey => 50 ) ;
            $this->setValidator( self::LABEL_FIELD, new StringValidator( $stringOptions ) ) ;
            $this->setValidator( self::RANK_FIELD , new IntegerValidator( array( IntegerValidator::$minKey => 0 ) ) ) ;
            $this->setValidator( self::SLUG_FIELD , new SlugValidator( $stringOptions ) ) ;

            $table = 'kp_roles';
            $this->setPostValidator(self::RANK_FIELD, new UniqueValidator(array( UniqueValidator::$fieldKey => self::RANK_FIELD, UniqueValidator::$tableKey => $table )) );
            $this->setPostValidator(self::SLUG_FIELD, new UniqueValidator(array( UniqueValidator::$fieldKey => self::SLUG_FIELD, UniqueValidator::$tableKey => $table )) );
        }

        /**
         * Returns the RoleVO of the model.
         *
         * @return RoleVO
         */
        public function getVO()
        {
            return parent::getVO();
        }
    }
?>